package com.shopping.service.impl;

import com.shopping.mapper.ProductMapper;
import com.shopping.pojo.Product;
import com.shopping.service.ProductService;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;


import java.util.List;

@Service
public class ProductServiceImpl implements ProductService {

    @Autowired
    private ProductMapper productMapper;

    @Override
    public List<Product> list() {
//        将产品image中url中文件名的空格给换成%20
        List<Product> list = productMapper.list();
        for (Product product : list) {
            String originalImage = product.getImage();
            // 直接将空格替换为%20
            String encodedImage = originalImage.replace(" ", "%20");
            product.setImage(encodedImage);
        }
        return list;
    }

    @Override
    public Product getById(Integer id) {
        return productMapper.getById(id);
    }

    @Override
    public boolean addProduct(Product product) {
        Product a=productMapper.getByName(product.getName());
        if(a!=null){
            return false;
        }
        else {
            productMapper.addProduct(product);
            return true;
        }
    }

    @Override
    public void deleteProduct(Integer id) {
        productMapper.deleteProduct(id);
    }

    @Override
    public boolean updateProduct(Product product) {

            productMapper.updateProduct(product);
            return true;

    }
    //添加该用户该商品的浏览记录
    @Override
    public void addBrowseLog(Product product, Integer customerId) {
        productMapper.addBrowseLog(product,customerId);
    }
}

